package cn.itcast.servlet;


import cn.itcast.datasource.MyDataSource;
import cn.itcast.domain.User;
import cn.itcast.utils.My_DataSource;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class Demo1 {

    public static void main(String[] args)  throws Exception{
        List<User> lis = new ArrayList<User>();
        MyDataSource dataSource = new MyDataSource();
        System.out.println("使用之前连接数量" + dataSource.getSize());
        //获取连接
        Connection conn = dataSource.getConnection();

        //定义sql
        String sql = "select * from user";

        //获取预编译对象
        PreparedStatement ps = conn.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();
        //获取结果集,并遍历

        while (rs.next()){
            System.out.println(rs.getInt("id") + "\t" + rs.getString("username") + "\t" + rs.getString("password"));
        }
        conn.close();
        ps.close();
        rs.close();

        System.out.println("使用之后连接数量" + dataSource.getSize());


    }
}
